package com.school.service;

import com.school.common.PageRequest;
import com.school.common.PageResult;
import com.school.common.ResultCode;
import com.school.dao.ClassDao;
import com.school.entity.Class;
import com.school.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班级服务类
 */
@Slf4j
@Service
public class ClassService {
    
    @Autowired
    private ClassDao classDao;
    
    /**
     * 添加班级
     */
    @Transactional(rollbackFor = Exception.class)
    public Long add(Class clazz) {
        // 验证班级名称是否已存在
        Class existClass = classDao.findByClassName(clazz.getClassName());
        if (existClass != null) {
            throw new BusinessException(ResultCode.CLASS_NAME_EXISTED);
        }
        
        return classDao.insert(clazz);
    }
    
    /**
     * 更新班级
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(Class clazz) {
        Class existClass = classDao.findById(clazz.getId());
        if (existClass == null) {
            throw new BusinessException(ResultCode.CLASS_NOT_EXIST);
        }
        
        classDao.update(clazz);
    }
    
    /**
     * 删除班级
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        Class existClass = classDao.findById(id);
        if (existClass == null) {
            throw new BusinessException(ResultCode.CLASS_NOT_EXIST);
        }
        
        classDao.deleteById(id);
    }
    
    /**
     * 批量删除班级
     */
    @Transactional(rollbackFor = Exception.class)
    public int batchDelete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("删除ID列表不能为空");
        }
        
        log.info("批量删除班级，数量: {}", ids.size());
        return classDao.batchDelete(ids);
    }
    
    /**
     * 根据ID查询班级
     */
    public Class getById(Long id) {
        Class clazz = classDao.findById(id);
        if (clazz == null) {
            throw new BusinessException(ResultCode.CLASS_NOT_EXIST);
        }
        return clazz;
    }
    
    /**
     * 根据班级名称查询
     */
    public Class getByClassName(String className) {
        return classDao.findByClassName(className);
    }
    
    /**
     * 查询所有班级
     */
    public List<Class> getAllClasses() {
        return classDao.findAll();
    }
    
    /**
     * 分页查询班级列表
     */
    public PageResult<Class> getPageList(String className, String major, String status, PageRequest pageRequest) {
        Map<String, Object> params = new HashMap<>();
        params.put("className", className);
        params.put("major", major);
        params.put("status", status);
        params.put("offset", pageRequest.getOffset());
        params.put("limit", pageRequest.getLimit());
        
        List<Class> list = classDao.findByPage(params);
        long total = classDao.count(params);
        
        return PageResult.of(pageRequest.getPageNum(), pageRequest.getPageSize(), total, list);
    }
    
    /**
     * 批量导入班级
     */
    @Transactional(rollbackFor = Exception.class)
    public int batchImport(List<Class> classes) {
        if (classes == null || classes.isEmpty()) {
            throw new BusinessException("导入数据不能为空");
        }
        
        int successCount = 0;
        for (Class clazz : classes) {
            // 验证必填字段
            if (clazz.getClassName() == null || clazz.getClassName().isEmpty()) {
                throw new BusinessException("班级名称不能为空");
            }
            if (clazz.getTeacherName() == null || clazz.getTeacherName().isEmpty()) {
                throw new BusinessException("班主任名称不能为空");
            }
            if (clazz.getPhone() == null || clazz.getPhone().isEmpty()) {
                throw new BusinessException("联系电话不能为空");
            }
            
            // 检查班级名称是否重复
            Class existClass = classDao.findByClassName(clazz.getClassName());
            if (existClass != null) {
                throw new BusinessException("班级名称已存在: " + clazz.getClassName());
            }
            
            classDao.insert(clazz);
            successCount++;
        }
        
        log.info("批量导入班级成功，数量: {}", successCount);
        return successCount;
    }
}


